﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrenzDenWebSite.Business;
using System.IO;

namespace FrenzDenWebSite
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetProfileInfo();
        }

        private void GetProfileInfo()
        {
            long profileId = 1000;
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                var result = from obj in dataContext.ProfileInfos
                             join m in dataContext.MediaLibraries on obj.ProfilePicID equals m.MediaID
                             where obj.ProfileID == profileId
                             select new { obj.FullName, obj.ProfilePicID, m.MediaPath };

                foreach (var item in result)
                {
                    imgProfile.ImageUrl = string.Format("~/media/pic/{0}", item.MediaPath);
                    imgPic.ImageUrl = string.Format("~/media/pic/{0}", item.MediaPath);
                    lblName.Text = item.FullName + " <img src=\"images/arrow-down-icon.png\" /> ";
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string ext = filePic.FileName.Substring(filePic.FileName.LastIndexOf(".")).ToLowerInvariant();
            if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif")
            {
                string fileName = Guid.NewGuid().ToString() + ext;
                string fullPath = Server.MapPath("~") + string.Format("/Media/Pic/{0}", fileName);
                filePic.SaveAs(fullPath);
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    dataContext.UploadPic(1000, fileName);
                }

                GetProfileInfo();
            }
            else
            {
                MessageBox msgbox = new MessageBox("Invalid file for the photo.");
                msgboxpanel.InnerHtml = msgbox.ReturnObject();
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Response.Cookies[SiteConstants.COOKIESTRINGPROFILE].Expires = DateTime.Now.AddHours(-2);
            Response.Redirect("~/Default.aspx");
        }
    }
}